#include "measurepagecharts.h"

MeasurePageCharts::MeasurePageCharts(int sample,QString waveName)
{
    this->sampleRate=sample;
    this->waveName=waveName;
    Series_wave = new QLineSeries;

    LineChartLen_X=5*sample;//一屏显示5秒钟的数据
    Chart_Y_Axis_Line_Color=new QColor(0xC4,0x91,0x44,0x20);
    Font_Chart_LabelsFont=new QFont("微软雅黑", 6, QFont::Normal, false);
    Font_Chart_TitleFont=new QFont("微软雅黑", 12, QFont::Normal, false);
    wave_flag=0;
    // y_maxv=1;
    // y_minv=-1;
    isAuto=true;//默认自动调整波形图
    InitialAxis_Chart();

}
MeasurePageCharts::~MeasurePageCharts()
{
    delete Series_wave;
    delete Chart_Y_Axis_Line_Color;
    delete Font_Chart_LabelsFont;
    delete Font_Chart_TitleFont;
    delete xAxis_Wave;
    delete yAxis_Wave;

}
void MeasurePageCharts::setWaveyRanger(float max,float min)
{
    y_maxv=max;
    y_minv=min;
    isAuto=false;

}
void MeasurePageCharts::InitialAxis_Chart()
{
    xAxis_Wave = new QValueAxis();
    yAxis_Wave = new QValueAxis();
    // yAxis_Wave->setRange(y_minv,y_maxv);
    xAxis_Wave->setRange(0, LineChartLen_X);
    xAxis_Wave->setTitleFont(*Font_Chart_TitleFont);
    yAxis_Wave->setTitleFont(*Font_Chart_TitleFont);
    xAxis_Wave->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_Wave->setLabelsFont(*Font_Chart_LabelsFont);
    yAxis_Wave->setTitleText("mv");
    yAxis_Wave->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_Wave->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_Wave->setGridLineVisible(false);
    yAxis_Wave->setGridLineVisible(true);
    yAxis_Wave->setGridLineColor(*Chart_Y_Axis_Line_Color);
    xAxis_Wave->setLineVisible(false);
    xAxis_Wave->setVisible(false);
    yAxis_Wave->setVisible(true);
}
/**
 * @brief MeasurePageCharts::CreateLineChart
 * @return
 */
QChart *MeasurePageCharts::CreateLineChart()
{
    QChart *chart = new QChart();
    Series_wave->setPen(QPen(QColor(0xFF,0x70,0x0F),2));//设置波形颜色
    chart->setTitle(waveName);
    chart->addSeries(Series_wave);
    chart->setBackgroundVisible(true);
//    chart->setBackgroundBrush(QBrush(QColor(0xF0,0xF6,0xF9)));
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_Wave, Qt::AlignBottom);
    chart->addAxis(yAxis_Wave, Qt::AlignLeft);
    Series_wave->attachAxis(xAxis_Wave);
    Series_wave->attachAxis(yAxis_Wave);
    return chart;
}
/**
 * @brief MeasurePageCharts::Update_Data
 * 功能：更新图表，绘图
 */
void MeasurePageCharts::Update_Data(bool iseleOff,double ppg)
{
    wave_flag++;
    QVector<QPointF> oldData = Series_wave->points();
    QVector<QPointF> data;
    if (oldData.size() < LineChartLen_X) {
        data = Series_wave->points();
    } else {
        for (int i = 1; i < oldData.size(); ++i) {
            data.append(QPointF(i - 1 , oldData.at(i).y()));
        }
    }
    qint64 size = data.size();
    data.append(QPointF(size, ppg));
    Series_wave->replace(data);

    //自动调整波形图表
    if(isAuto){
        if(wave_flag%100==0){
            double maxv=10;
            double minv=-10;
            if(!iseleOff){
                maxv=data.at(0).y();
                minv=data.at(0).y();
            }

            for(int i=0;i<size;i++)
            {
                if(data.at(i).y()>maxv)
                    maxv=data.at(i).y();
                if(data.at(i).y()<minv)
                    minv=data.at(i).y();
            }
            yAxis_Wave->setRange(minv,maxv);
        }
    }else
    {
        if(wave_flag%100==0){
            yAxis_Wave->setRange(y_minv,y_maxv);
        }
    }

}
void MeasurePageCharts::clearWave()
{
    Series_wave->clear();
}


